<?php

/**
 * Clientorder form.
 *
 * @package    briefingPropel
 * @subpackage form
 * @author     Your name here
 */
class ClientorderForm extends BaseClientorderForm
{
  public function configure()
  {   
    
    parent::configure();
    
    //hacemos desconexión de los campos que deseemos.
      //La desconexión de un campo significa que tanto el widget como validador se eliminan.
    unset(
      $this['created_at'], $this['updated_at'],
      $this['expires_at']
            
    );
    
    
    //change labels
    $this->widgetSchema->setLabels(array(
        'orderName' => 'Order name',
        'textCategoryId' => 'Category',
        'targetGroupId'=>'Target group',
        'formalStyleId'=>'Style',
        'personStyleId'=>'Person form',
        'emotionStyleId'=>'Emotion style',
        'formulateId'=>'Formulate',
//        'freeTextId'=>'Free text',
//        'editionOptionsId'=>'Edition options',
//        'otherTextMediumId' => 'XX'
    ));
    
    //is_standard
    $this->widgetSchema['is_standard'] = new sfWidgetFormInputCheckbox(array(
        'label' => 'Standard',
        'default' => true,
    ));
    $this->validatorSchema['is_standard'] = new sfValidatorBoolean();
    
    //MEDIA
    //is_website
    $this->widgetSchema['is_website'] = new sfWidgetFormInputCheckbox(array(
        'label' => 'Website'
    ));
    $this->validatorSchema['is_website'] = new sfValidatorBoolean(); 
    //is_print
    $this->widgetSchema['is_print'] = new sfWidgetFormInputCheckbox(array(
        'label' => 'Print'
    ));
    $this->validatorSchema['is_print'] = new sfValidatorBoolean();
    //is_newsletter
    $this->widgetSchema['is_newsletter'] = new sfWidgetFormInputCheckbox(array(
        'label' => 'Newsletter'
    ));
    $this->validatorSchema['is_newsletter'] = new sfValidatorBoolean();
    //is_poster
    $this->widgetSchema['is_poster'] = new sfWidgetFormInputCheckbox(array(
        'label' => 'Poster'
    ));
    $this->validatorSchema['is_poster'] = new sfValidatorBoolean();
    //is_blog
    $this->widgetSchema['is_blog'] = new sfWidgetFormInputCheckbox(array(
        'label' => 'Blog'
    ));
    $this->validatorSchema['is_blog'] = new sfValidatorBoolean();
    
    //TARGET GROUP
    //is_men
    $this->widgetSchema['is_men'] = new sfWidgetFormInputCheckbox(array(
        'label' => 'Men'
    ));
    $this->validatorSchema['is_men'] = new sfValidatorBoolean();
    
    //is_women
    $this->widgetSchema['is_women'] = new sfWidgetFormInputCheckbox(array(
        'label' => 'Women'
    ));
    $this->validatorSchema['is_women'] = new sfValidatorBoolean();
    
    //is_existing_custormer
    $this->widgetSchema['is_existing_custormer'] = new sfWidgetFormInputCheckbox(array(
        'label' => 'Existing customer'
    ));
    $this->validatorSchema['is_existing_custormer'] = new sfValidatorBoolean();
    
    //is_new_customer
    $this->widgetSchema['is_new_customer'] = new sfWidgetFormInputCheckbox(array(
        'label' => 'New customer'
    ));
    $this->validatorSchema['is_new_customer'] = new sfValidatorBoolean();
    
    
    //free text
    $this->widgetSchema['freeTextId'] = new sfWidgetFormInputHidden();
 
    
    //IMAGE
    $this->widgetSchema['urlImageStructure'] = new sfWidgetFormInputFile(array(
        'label' => 'Company logo order',
    ));
    
    $this->validatorSchema['urlImageStructure'] = new sfValidatorFile(array(
      'required'   => false,
      'path'       => sfConfig::get('sf_upload_dir').'/orders',
      'mime_types' => 'web_images',
    ));
    
    //HELP
    $this->widgetSchema->setHelp('is_standard', 'Is it an standard order?');
    
    
    
    
    //free text  
    $freeText;
    $formFreeText;
    
    $savedFreeText = FreetextPeer::retrieveByPk($this->getObject()->getFreetextid());
    
    if(is_null($savedFreeText))
    {
      $freeText = new Freetext();
      $formFreeText = new FreetextForm($freeText);
    }
    else
    {
      $formFreeText = new FreetextForm($savedFreeText);
    }
    
    $this->embedForm('Free text', $formFreeText);
    
    //edition options
    $editionOptions;
    $formEditionOptions;
    
    $editionOptions = EditionoptionsPeer::retrieveByPk($this->getObject()->getEditionoptionsid());
    
    if(is_null($editionOptions))
    {
      $edOptions = new Editionoptions();
      $formEditionOptions = new EditionoptionsForm($edOptions);
    }
    else
    {
      $formEditionOptions = new EditionoptionsForm($editionOptions);
    }
    
    $this->embedForm('Edition options', $formEditionOptions);
    
    
    //Other media
    $formOtherMedia;
    $textMedia;

    //including the Other text medium form
    $savedMedia = OthertextmediumPeer::retrieveByPk($this->getObject()->getOthertextmediumid());

    if(is_null($savedMedia))
    {//new orders dont have an "other text media selection" choose
      //echo "deberia ser un NEW...";
      
      $textMedia = new Othertextmedium();
      $formOtherMedia = new OthertextmediumForm($textMedia);//OthertextmediumPeer::retrieveByPk($this->getObject()->getOthertextmediumid()));
    }
    else
    {
      //echo "deberia ser un EDIT-> ".$savedMedia->getName();
      
      $formOtherMedia = new OthertextmediumForm($savedMedia);//OthertextmediumPeer::retrieveByPk($this->getObject()->getOthertextmediumid()));
    }
    
    $this->embedForm('Other text media', $formOtherMedia);//'otherTextMediumId', $formOtherMedia);
    
  }
  
  protected function doSave($con = null)
  {
 
    
    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $this->updateObject(
            $this->getValues()
            );
    //gets called first
    $this->saveEmbeddedForms($con);

    //save all the embedded objects
    $this->getObject()->setOthertextmedium($this->getEmbeddedForm('Other text media')->getObject());
    $this->getObject()->setEditionoptions($this->getEmbeddedForm('Edition options')->getObject());
    $this->getObject()->setFreetext($this->getEmbeddedForm('Free text')->getObject());
        
    //object is saved after embedded form
    $this->getObject()->save($con);

  }
  
  //necesary for calling the doSave of the embedded of an embedded form (recursivelly)
  public function saveEmbeddedForms($con = null, $forms = null)
  {
    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    if (is_null($forms))
    {
      $forms = $this->embeddedForms;
    }

    foreach ($forms as $key => $form)
    {
      if ($form instanceof sfFormPropel)
      {
        $form->bind($this->values[$key],$this->taintedFiles);//added the $this->taintedFiles for the image of the free text
        $form->doSave($con);
        $form->saveEmbeddedForms($con);
      }
      else
      {
        $this->saveEmbeddedForms($con, $form->getEmbeddedForms());
      }
    }
  } 
}
